home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / volume4 / cribbage / part02 < prev   
Encoding:
Internet Message Format  |  1988-02-28  |  22.7 KB

  1. Path: uunet!tektronix!tekgen!tekred!games
  2. From: games@tekred.TEK.COM
  3. Newsgroups: comp.sources.games
  4. Subject: v04i004:  cribbage - 4.3BSD cribbage game, Part02/02
  5. Message-ID: <2374@tekred.TEK.COM>
  6. Date: 31 Mar 88 21:55:01 GMT
  7. Sender: billr@tekred.TEK.COM
  8. Lines: 942
  9. Approved: billr@saab.CNA.TEK.COM
  10.  
  11. Submitted by: bostic@okeeffe.Berkeley.EDU (Keith Bostic)
  12. Comp.sources.games: Volume 4, Issue 4
  13. Archive-name: cribbage/Part02
  14.  
  15.  
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of archive 2 (of 2)."
  24. # Contents:  Makefile cards.c cribbage.h deck.h extern.c macro
  25. #   support.c
  26. # Wrapped by billr@saab on Thu Mar 31 13:51:53 1988
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f Makefile -a "${1}" != "-c" ; then 
  29.   echo shar: Will not over-write existing file \"Makefile\"
  30. else
  31. echo shar: Extracting \"Makefile\" \(2563 characters\)
  32. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  33. X#
  34. X# Copyright (c) 1987 Regents of the University of California.
  35. X# All rights reserved.
  36. X#
  37. X# Redistribution and use in source and binary forms are permitted
  38. X# provided that this notice is preserved and that due credit is given
  39. X# to the University of California at Berkeley. The name of the University
  40. X# may not be used to endorse or promote products derived from this
  41. X# software without specific prior written permission. This software
  42. X# is provided ``as is'' without express or implied warranty.
  43. X#
  44. X#    @(#)Makefile    5.6 (Berkeley) 3/10/88
  45. X#
  46. XCFLAGS=    -O
  47. XLIBC=    /lib/libc.a
  48. XLIBS=    -lcurses -ltermlib
  49. XHDRS=    cribbage.h deck.h cribcur.h
  50. XSRCS=    extern.c crib.c support.c cards.c score.c io.c
  51. XOBJS=    extern.o crib.o support.o cards.o score.o io.o
  52. XTOBJS=    test.o cards.o score.o io.o extern.o
  53. X
  54. Xall: cribbage crib.instr
  55. X
  56. Xcribbage: ${OBJS} ${LIBC}
  57. X    ${CC} -o $@ ${CFLAGS} ${OBJS} ${LIBS}
  58. X
  59. Xtest: ${TOBJS} ${LIBC}
  60. X    ${CC} -o $@ ${CFLAGS} ${TOBJS} ${LIBS}
  61. X
  62. Xcrib.instr: cribbage.n macro
  63. X    nroff cribbage.n > crib.instr
  64. X
  65. Xclean: FRC
  66. X    rm -f ${OBJS} core crib.instr cribbage test
  67. X
  68. Xdepend: FRC
  69. X    mkdep ${CFLAGS} ${SRCS}
  70. X
  71. Xinstall: FRC
  72. X    install -o games -g bin -m 400 crib.instr ${DESTDIR}/usr/games/lib/crib.instr
  73. X    install -s -o games -g bin -m 4700 cribbage ${DESTDIR}/usr/games/hide
  74. X    (cd ${DESTDIR}/usr/games; rm -f cribbage; ln -s dm cribbage; chown games.bin cribbage)
  75. X
  76. Xlint: FRC
  77. X    lint ${CFLAGS} ${SRCS}
  78. X
  79. Xtags: FRC
  80. X    ctags ${SRCS}
  81. X
  82. XFRC:
  83. X
  84. X# DO NOT DELETE THIS LINE -- mkdep uses it.
  85. X# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
  86. X
  87. Xextern.o: extern.c /usr/include/curses.h /usr/include/stdio.h
  88. Xextern.o: /usr/include/sgtty.h /usr/include/sys/ioctl.h
  89. Xextern.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h deck.h
  90. Xextern.o: cribbage.h
  91. Xcrib.o: crib.c /usr/include/curses.h /usr/include/stdio.h /usr/include/sgtty.h
  92. Xcrib.o: /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h
  93. Xcrib.o: /usr/include/sys/ttydev.h /usr/include/signal.h deck.h cribbage.h
  94. Xcrib.o: cribcur.h
  95. Xsupport.o: support.c /usr/include/curses.h /usr/include/stdio.h
  96. Xsupport.o: /usr/include/sgtty.h /usr/include/sys/ioctl.h
  97. Xsupport.o: /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h deck.h
  98. Xsupport.o: cribbage.h cribcur.h
  99. Xcards.o: cards.c /usr/include/stdio.h deck.h
  100. Xscore.o: score.c /usr/include/stdio.h deck.h cribbage.h
  101. Xio.o: io.c /usr/include/curses.h /usr/include/stdio.h /usr/include/sgtty.h
  102. Xio.o: /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h
  103. Xio.o: /usr/include/sys/ttydev.h /usr/include/ctype.h /usr/include/signal.h
  104. Xio.o: deck.h cribbage.h cribcur.h
  105. X
  106. X# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
  107. END_OF_Makefile
  108. if test 2563 -ne `wc -c <Makefile`; then
  109.     echo shar: \"Makefile\" unpacked with wrong size!
  110. fi
  111. # end of overwriting check
  112. fi
  113. if test -f cards.c -a "${1}" != "-c" ; then 
  114.   echo shar: Will not over-write existing file \"cards.c\"
  115. else
  116. echo shar: Extracting \"cards.c\" \(2337 characters\)
  117. sed "s/^X//" >cards.c <<'END_OF_cards.c'
  118. X/*
  119. X * Copyright (c) 1980 Regents of the University of California.
  120. X * All rights reserved.
  121. X *
  122. X * Redistribution and use in source and binary forms are permitted
  123. X * provided that this notice is preserved and that due credit is given
  124. X * to the University of California at Berkeley. The name of the University
  125. X * may not be used to endorse or promote products derived from this
  126. X * software without specific prior written permission. This software
  127. X * is provided ``as is'' without express or implied warranty.
  128. X */
  129. X
  130. X#ifndef lint
  131. Xstatic char sccsid[] = "@(#)cards.c    5.2 (Berkeley) 3/10/88";
  132. X#endif /* not lint */
  133. X
  134. X#include    <stdio.h>
  135. X#include    "deck.h"
  136. X
  137. X
  138. X/*
  139. X * initialize a deck of cards to contain one of each type
  140. X */
  141. X
  142. Xmakedeck( d )
  143. X
  144. X    CARD    d[];
  145. X{
  146. X    register  int        i, j, k;
  147. X    long            time();
  148. X
  149. X    i = time( (long *) 0 );
  150. X    i = ( (i&0xff) << 8 ) | ( (i >> 8)&0xff ) | 1;
  151. X    srand( i );
  152. X    k = 0;
  153. X    for( i = 0; i < RANKS; i++ )  {
  154. X        for( j = 0; j < SUITS; j++ )  {
  155. X        d[k].suit = j;
  156. X        d[k++].rank = i;
  157. X        }
  158. X    }
  159. X}
  160. X
  161. X
  162. X
  163. X/*
  164. X * given a deck of cards, shuffle it -- i.e. randomize it
  165. X * see Knuth, vol. 2, page 125
  166. X */
  167. X
  168. Xshuffle( d )
  169. X
  170. X    CARD    d[];
  171. X{
  172. X    register  int        j, k;
  173. X    CARD            c;
  174. X
  175. X    for( j = CARDS; j > 0; --j )  {
  176. X        k = ( rand() >> 4 ) % j;        /* random 0 <= k < j */
  177. X        c = d[j - 1];            /* exchange (j - 1) and k */
  178. X        d[j - 1] = d[k];
  179. X        d[k] = c;
  180. X    }
  181. X}
  182. X
  183. X
  184. X
  185. X/*
  186. X * return true if the two cards are equal...
  187. X */
  188. X
  189. Xeq( a, b )
  190. X
  191. X    CARD        a, b;
  192. X{
  193. X    return(  ( a.rank == b.rank )  &&  ( a.suit == b.suit )  );
  194. X}
  195. X
  196. X
  197. X
  198. X/*
  199. X * isone returns TRUE if a is in the set of cards b
  200. X */
  201. X
  202. Xisone( a, b, n )
  203. X
  204. X    CARD        a, b[];
  205. X    int            n;
  206. X{
  207. X    register  int        i;
  208. X
  209. X    for( i = 0; i < n; i++ )  {
  210. X        if(  eq( a, b[i] )   )  return( TRUE );
  211. X    }
  212. X    return( FALSE );
  213. X}
  214. X
  215. X
  216. X
  217. X/*
  218. X * remove the card a from the deck d of n cards
  219. X */
  220. X
  221. Xremove( a, d, n )
  222. X
  223. X    CARD        a, d[];
  224. X    int            n;
  225. X{
  226. X    register  int        i, j;
  227. X
  228. X    j = 0;
  229. X    for( i = 0; i < n; i++ )  {
  230. X        if(  !eq( a, d[i] )  )  d[j++] = d[i];
  231. X    }
  232. X    if(  j < n  )  d[j].suit = d[j].rank = EMPTY;
  233. X}
  234. X
  235. X
  236. X
  237. X/*
  238. X * sorthand:
  239. X *    Sort a hand of n cards
  240. X */
  241. Xsorthand(h, n)
  242. Xregister CARD        h[];
  243. Xint            n;
  244. X{
  245. X    register CARD        *cp, *endp;
  246. X    CARD            c;
  247. X
  248. X    for (endp = &h[n]; h < endp - 1; h++)
  249. X        for (cp = h + 1; cp < endp; cp++)
  250. X        if ((cp->rank < h->rank) ||
  251. X             (cp->rank == h->rank && cp->suit < h->suit)) {
  252. X            c = *h;
  253. X            *h = *cp;
  254. X            *cp = c;
  255. X        }
  256. X}
  257. X
  258. END_OF_cards.c
  259. if test 2337 -ne `wc -c <cards.c`; then
  260.     echo shar: \"cards.c\" unpacked with wrong size!
  261. fi
  262. # end of overwriting check
  263. fi
  264. if test -f cribbage.h -a "${1}" != "-c" ; then 
  265.   echo shar: Will not over-write existing file \"cribbage.h\"
  266. else
  267. echo shar: Extracting \"cribbage.h\" \(1557 characters\)
  268. sed "s/^X//" >cribbage.h <<'END_OF_cribbage.h'
  269. X/*
  270. X * Copyright (c) 1980 Regents of the University of California.
  271. X * All rights reserved.
  272. X *
  273. X * Redistribution and use in source and binary forms are permitted
  274. X * provided that this notice is preserved and that due credit is given
  275. X * to the University of California at Berkeley. The name of the University
  276. X * may not be used to endorse or promote products derived from this
  277. X * software without specific prior written permission. This software
  278. X * is provided ``as is'' without express or implied warranty.
  279. X *
  280. X *    @(#)cribbage.h    5.2 (Berkeley) 3/10/88
  281. X */
  282. X
  283. Xextern  CARD        deck[ CARDS ];        /* a deck */
  284. Xextern  CARD        phand[ FULLHAND ];    /* player's hand */
  285. Xextern  CARD        chand[ FULLHAND ];    /* computer's hand */
  286. Xextern  CARD        crib[ CINHAND ];    /* the crib */
  287. Xextern  CARD        turnover;        /* the starter */
  288. X
  289. Xextern  CARD        known[ CARDS ];        /* cards we have seen */
  290. Xextern  int        knownum;        /* # of cards we know */
  291. X
  292. Xextern  int        pscore;            /* player's score */
  293. Xextern  int        cscore;            /* comp's score */
  294. Xextern  int        glimit;            /* points to win game */
  295. X
  296. Xextern  int        pgames;            /* player's games won */
  297. Xextern  int        cgames;            /* comp's games won */
  298. Xextern  int        gamecount;        /* # games played */
  299. Xextern    int        Lastscore[2];        /* previous score for each */
  300. X
  301. Xextern  BOOLEAN        iwon;            /* if comp won last */
  302. Xextern  BOOLEAN        explain;        /* player mistakes explained */
  303. Xextern  BOOLEAN        rflag;            /* if all cuts random */
  304. Xextern  BOOLEAN        quiet;            /* if suppress random mess */
  305. Xextern    BOOLEAN        playing;        /* currently playing game */
  306. X
  307. Xextern  char        expl[];            /* string for explanation */
  308. X
  309. END_OF_cribbage.h
  310. if test 1557 -ne `wc -c <cribbage.h`; then
  311.     echo shar: \"cribbage.h\" unpacked with wrong size!
  312. fi
  313. # end of overwriting check
  314. fi
  315. if test -f deck.h -a "${1}" != "-c" ; then 
  316.   echo shar: Will not over-write existing file \"deck.h\"
  317. else
  318. echo shar: Extracting \"deck.h\" \(1549 characters\)
  319. sed "s/^X//" >deck.h <<'END_OF_deck.h'
  320. X/*
  321. X * Copyright (c) 1980 Regents of the University of California.
  322. X * All rights reserved.
  323. X *
  324. X * Redistribution and use in source and binary forms are permitted
  325. X * provided that this notice is preserved and that due credit is given
  326. X * to the University of California at Berkeley. The name of the University
  327. X * may not be used to endorse or promote products derived from this
  328. X * software without specific prior written permission. This software
  329. X * is provided ``as is'' without express or implied warranty.
  330. X *
  331. X *    @(#)deck.h    5.2 (Berkeley) 3/10/88
  332. X */
  333. X
  334. X/*
  335. X * define structure of a deck of cards and other related things
  336. X */
  337. X
  338. X
  339. X#define        CARDS        52        /* number cards in deck */
  340. X#define        RANKS        13        /* number ranks in deck */
  341. X#define        SUITS        4        /* number suits in deck */
  342. X
  343. X#define        CINHAND        4        /* # cards in cribbage hand */
  344. X#define        FULLHAND    6        /* # cards in dealt hand */
  345. X
  346. X#define        LGAME        121        /* number points in a game */
  347. X#define        SGAME        61        /* # points in a short game */
  348. X
  349. X#define        SPADES        0        /* value of each suit */
  350. X#define        HEARTS        1
  351. X#define        DIAMONDS    2
  352. X#define        CLUBS        3
  353. X
  354. X#define        ACE        0        /* value of each rank */
  355. X#define        TWO        1
  356. X#define        THREE        2
  357. X#define        FOUR        3
  358. X#define        FIVE        4
  359. X#define        SIX        5
  360. X#define        SEVEN        6
  361. X#define        EIGHT        7
  362. X#define        NINE        8
  363. X#define        TEN        9
  364. X#define        JACK        10
  365. X#define        QUEEN        11
  366. X#define        KING        12
  367. X#define        EMPTY        13
  368. X
  369. X#define        VAL(c)        ( (c) < 9 ? (c)+1 : 10 )    /* val of rank */
  370. X
  371. X
  372. X#ifndef TRUE
  373. X#    define        TRUE        1
  374. X#    define        FALSE        0
  375. X#endif
  376. X
  377. Xtypedef        struct  {
  378. X            int        rank;
  379. X            int        suit;
  380. X        }        CARD;
  381. X
  382. Xtypedef        char        BOOLEAN;
  383. X
  384. END_OF_deck.h
  385. if test 1549 -ne `wc -c <deck.h`; then
  386.     echo shar: \"deck.h\" unpacked with wrong size!
  387. fi
  388. # end of overwriting check
  389. fi
  390. if test -f extern.c -a "${1}" != "-c" ; then 
  391.   echo shar: Will not over-write existing file \"extern.c\"
  392. else
  393. echo shar: Extracting \"extern.c\" \(1667 characters\)
  394. sed "s/^X//" >extern.c <<'END_OF_extern.c'
  395. X/*
  396. X * Copyright (c) 1980 Regents of the University of California.
  397. X * All rights reserved.
  398. X *
  399. X * Redistribution and use in source and binary forms are permitted
  400. X * provided that this notice is preserved and that due credit is given
  401. X * to the University of California at Berkeley. The name of the University
  402. X * may not be used to endorse or promote products derived from this
  403. X * software without specific prior written permission. This software
  404. X * is provided ``as is'' without express or implied warranty.
  405. X */
  406. X
  407. X#ifndef lint
  408. Xstatic char sccsid[] = "@(#)extern.c    5.2 (Berkeley) 3/10/88";
  409. X#endif /* not lint */
  410. X
  411. X# include    <curses.h>
  412. X# include    "deck.h"
  413. X# include    "cribbage.h"
  414. X
  415. Xbool    explain        = FALSE;    /* player mistakes explained */
  416. Xbool    iwon        = FALSE;    /* if comp won last game */
  417. Xbool    quiet        = FALSE;    /* if suppress random mess */
  418. Xbool    rflag        = FALSE;    /* if all cuts random */
  419. X
  420. Xchar    expl[128];            /* explanation */
  421. X
  422. Xint    cgames        = 0;        /* number games comp won */
  423. Xint    cscore        = 0;        /* comp score in this game */
  424. Xint    gamecount    = 0;        /* number games played */
  425. Xint    glimit        = LGAME;    /* game playe to glimit */
  426. Xint    knownum        = 0;        /* number of cards we know */
  427. Xint    pgames        = 0;        /* number games player won */
  428. Xint    pscore        = 0;        /* player score in this game */
  429. X
  430. XCARD    chand[FULLHAND];        /* computer's hand */
  431. XCARD    crib[CINHAND];            /* the crib */
  432. XCARD    deck[CARDS];            /* a deck */
  433. XCARD    known[CARDS];            /* cards we have seen */
  434. XCARD    phand[FULLHAND];        /* player's hand */
  435. XCARD    turnover;            /* the starter */
  436. X
  437. XWINDOW    *Compwin;            /* computer's hand window */
  438. XWINDOW    *Msgwin;            /* messages for the player */
  439. XWINDOW    *Playwin;            /* player's hand window */
  440. XWINDOW    *Tablewin;            /* table window */
  441. END_OF_extern.c
  442. if test 1667 -ne `wc -c <extern.c`; then
  443.     echo shar: \"extern.c\" unpacked with wrong size!
  444. fi
  445. # end of overwriting check
  446. fi
  447. if test -f macro -a "${1}" != "-c" ; then 
  448.   echo shar: Will not over-write existing file \"macro\"
  449. else
  450. echo shar: Extracting \"macro\" \(2028 characters\)
  451. sed "s/^X//" >macro <<'END_OF_macro'
  452. X.\" Copyright (c) 1980 Regents of the University of California.
  453. X.\" All rights reserved.
  454. X.\"
  455. X.\" Redistribution and use in source and binary forms are permitted
  456. X.\" provided that this notice is preserved and that due credit is given
  457. X.\" to the University of California at Berkeley. The name of the University
  458. X.\" may not be used to endorse or promote products derived from this
  459. X.\" software without specific prior written permission. This software
  460. X.\" is provided ``as is'' without express or implied warranty.
  461. X.\"
  462. X.\"    @(#)macro    5.2 (Berkeley) 3/10/88
  463. X.\"
  464. X.nr  TX  2    \" default line space for text is 2
  465. X.nr  PG  8    \" default paragraph indentation in PG is 8
  466. X.nr  QI  8    \" default indentation for quotes is 8
  467. X.nr  IP  4    \" default indentation for indented paragraph is 4
  468. X.de  TX        \" define text settings
  469. X.ls \\n(TX
  470. X.fi
  471. X..
  472. X.de  NT        \" define non-text setings
  473. X.nf
  474. X.ls 1
  475. X..
  476. X.de  PG        \" define paragraph starter
  477. X.sp
  478. X.ti +\\n(PG
  479. X..
  480. X.de  HD        \" define page header
  481. X'sp 1i
  482. X..
  483. X.de  FO        \" define page footer
  484. X.if \\n%>1 \{\
  485. X'sp 2
  486. X.tl ""- % -""     \}
  487. X'bp
  488. X..
  489. X.wh 0 HD    \" trap invocation of header
  490. X.wh -1i FO    \" trap invocation of footer
  491. X.de  pH        \" define page header
  492. X.ce
  493. X.ul
  494. X\\$1
  495. X.sp 1
  496. X..
  497. X.de  PF        \" define Page header for first page -- no .bp
  498. X.ce
  499. X.ul
  500. X\\$1
  501. X.sp 1
  502. X..
  503. X.de  PH        \" define Page header
  504. X.ce
  505. X.ul
  506. X\\$1
  507. X.sp 1
  508. X..
  509. X.de  HP        \" define paragraph header
  510. X.sp 2
  511. X.ne 4
  512. X.ul
  513. X\\$1
  514. X.PG
  515. X..
  516. X.de  TP        \" define title page
  517. X.ls  1
  518. X.sp 3.5i
  519. X.ce
  520. X.ul
  521. X\\$1
  522. X.sp +3i
  523. X.ti +50
  524. X\\$2
  525. X.br
  526. X.ti +50
  527. X\\$3
  528. X.br
  529. X.ti +50
  530. X\\n(mo / \\n(dy  / \\n(yr
  531. X.nr  %  0
  532. X.ls
  533. X..
  534. X.de  KS
  535. X.br
  536. X.di  KB
  537. X..
  538. X.de  KE
  539. X.br
  540. X.di
  541. X.ne  \\n(dnu
  542. X.KB
  543. X..
  544. X.de  QI        \" define indented quote macro
  545. X.sp
  546. X.in +\\n(QI
  547. X.ll -\\n(QI
  548. X.ls 1
  549. X..
  550. X.de  EQ        \" define end quote macro
  551. X.ls
  552. X.sp
  553. X.ll +\\n(QI
  554. X.in -\\n(QI
  555. X..
  556. X.de  IP        \" define indented paragraph macro
  557. X.in +\\n(IP
  558. X.ll -\\n(IP
  559. X.PG
  560. X..
  561. X.de  EP        \" define end indented paragraph macro
  562. X.ll +\\n(IP
  563. X.in -\\n(IP
  564. X..
  565. X.de  IH        \" define indented paragraph header
  566. X.in +\\n(IP
  567. X.ll -\\n(IP
  568. X.sp 2
  569. X.ne 4
  570. X.ul
  571. X\\$1
  572. X.PG
  573. X..
  574. X.de IN
  575. X.in +8
  576. X.br
  577. X.ti -4
  578. X\\$1
  579. X..
  580. X.de NE
  581. X.in -8
  582. X..
  583. END_OF_macro
  584. if test 2028 -ne `wc -c <macro`; then
  585.     echo shar: \"macro\" unpacked with wrong size!
  586. fi
  587. # end of overwriting check
  588. fi
  589. if test -f support.c -a "${1}" != "-c" ; then 
  590.   echo shar: Will not over-write existing file \"support.c\"
  591. else
  592. echo shar: Extracting \"support.c\" \(6894 characters\)
  593. sed "s/^X//" >support.c <<'END_OF_support.c'
  594. X/*
  595. X * Copyright (c) 1980 Regents of the University of California.
  596. X * All rights reserved.
  597. X *
  598. X * Redistribution and use in source and binary forms are permitted
  599. X * provided that this notice is preserved and that due credit is given
  600. X * to the University of California at Berkeley. The name of the University
  601. X * may not be used to endorse or promote products derived from this
  602. X * software without specific prior written permission. This software
  603. X * is provided ``as is'' without express or implied warranty.
  604. X */
  605. X
  606. X#ifndef lint
  607. Xstatic char sccsid[] = "@(#)support.c    5.3 (Berkeley) 3/10/88";
  608. X#endif /* not lint */
  609. X
  610. X#include    <curses.h>
  611. X#include    "deck.h"
  612. X#include    "cribbage.h"
  613. X#include    "cribcur.h"
  614. X
  615. X
  616. X#define        NTV        10        /* number scores to test */
  617. X
  618. X/* score to test reachability of, and order to test them in */
  619. Xint        tv[ NTV ]    = { 8, 7, 9, 6, 11, 12, 13, 14, 10, 5 };
  620. X
  621. X
  622. X/*
  623. X * computer chooses what to play in pegging...
  624. X * only called if no playable card will score points
  625. X */
  626. X
  627. Xcchose( h, n, s )
  628. X
  629. X    CARD        h[];
  630. X    int            n;
  631. X    int            s;
  632. X{
  633. X    register  int        i, j, l;
  634. X
  635. X    if(  n <= 1  )  return( 0 );
  636. X    if(  s < 4  )  {        /* try for good value */
  637. X        if(  ( j = anysumto(h, n, s, 4) )  >=  0  )  return( j );
  638. X        if(  ( j = anysumto(h, n, s, 3) ) >= 0  &&  s == 0  )
  639. X                                return( j );
  640. X    }
  641. X    if(  s > 0  &&  s < 20  )  {
  642. X        for( i = 1; i <= 10; i++ )  {    /* try for retaliation to 31 */
  643. X        if(  ( j = anysumto(h, n, s, 21-i) )  >=  0  )  {
  644. X            if(  ( l = numofval(h, n, i) )  >  0  )  {
  645. X            if(  l > 1  ||  VAL( h[j].rank ) != i  )  return( j );
  646. X            }
  647. X        }
  648. X        }
  649. X    }
  650. X    if(  s < 15  )  {
  651. X        for( i = 0; i < NTV; i++ )  {    /* for retaliation after 15 */
  652. X        if(  ( j = anysumto(h, n, s, tv[i]) )  >=  0  )  {
  653. X            if(  ( l = numofval(h, n, 15-tv[i]) )  >  0  )  {
  654. X            if(  l > 1  ||  VAL( h[j].rank ) != 15-tv[i]  )  return( j );
  655. X            }
  656. X        }
  657. X        }
  658. X    }
  659. X    j = -1;
  660. X    for( i = n - 1; i >= 0; --i )  {    /* remember: h is sorted */
  661. X        l = s + VAL( h[i].rank );
  662. X        if(  l > 31  )  continue;
  663. X        if(  l != 5  &&  l != 10  &&  l != 21  )  {
  664. X        j = i;
  665. X        break;
  666. X        }
  667. X    }
  668. X    if(  j >= 0  )  return( j );
  669. X    for( i = n - 1; i >= 0; --i )  {
  670. X        l = s + VAL( h[i].rank );
  671. X        if(  l > 31  )  continue;
  672. X        if(  j < 0  )  j = i;
  673. X        if(  l != 5  &&  l != 21  )  {
  674. X        j = i;
  675. X        break;
  676. X        }
  677. X    }
  678. X    return( j );
  679. X}
  680. X
  681. X
  682. X
  683. X/*
  684. X * plyrhand:
  685. X *    Evaluate and score a player hand or crib
  686. X */
  687. Xplyrhand(hand, s)
  688. XCARD        hand[];
  689. Xchar        *s;
  690. X{
  691. X    register int    i, j;
  692. X    register BOOLEAN    win;
  693. X    static char        prompt[BUFSIZ];
  694. X
  695. X    prhand(hand, CINHAND, Playwin, FALSE);
  696. X    (void)sprintf(prompt, "Your %s scores ", s);
  697. X    i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
  698. X    if ((j = number(0, 29, prompt)) == 19)
  699. X    j = 0;
  700. X    if (i != j) {
  701. X    if (i < j) {
  702. X        win = chkscr(&pscore, i);
  703. X        msg("It's really only %d points; I get %d", i, 2);
  704. X        if (!win)
  705. X        win = chkscr(&cscore, 2);
  706. X    }
  707. X    else {
  708. X        win = chkscr(&pscore, j);
  709. X        msg("You should have taken %d, not %d!", i, j);
  710. X    }
  711. X    if (explain)
  712. X        msg("Explanation: %s", expl);
  713. X    do_wait();
  714. X    }
  715. X    else
  716. X    win = chkscr(&pscore, i);
  717. X    return win;
  718. X}
  719. X
  720. X/*
  721. X * comphand:
  722. X *    Handle scoring and displaying the computers hand
  723. X */
  724. Xcomphand(h, s)
  725. XCARD        h[];
  726. Xchar        *s;
  727. X{
  728. X    register int        j;
  729. X
  730. X    j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, FALSE);
  731. X    prhand(h, CINHAND, Compwin, FALSE);
  732. X    msg("My %s scores %d", s, (j == 0 ? 19 : j));
  733. X    return chkscr(&cscore, j);
  734. X}
  735. X
  736. X/*
  737. X * chkscr:
  738. X *    Add inc to scr and test for > glimit, printing on the scoring
  739. X *    board while we're at it.
  740. X */
  741. X
  742. Xint    Lastscore[2] = {-1, -1};
  743. X
  744. Xchkscr(scr, inc)
  745. Xint        *scr, inc;
  746. X{
  747. X    BOOLEAN        myturn;
  748. X
  749. X    myturn = (scr == &cscore);
  750. X    if (inc != 0) {
  751. X        prpeg(Lastscore[myturn], '.', myturn);
  752. X        Lastscore[myturn] = *scr;
  753. X        *scr += inc;
  754. X        prpeg(*scr, PEG, myturn);
  755. X        refresh();
  756. X    }
  757. X    return (*scr >= glimit);
  758. X}
  759. X
  760. X/*
  761. X * prpeg:
  762. X *    Put out the peg character on the score board and put the
  763. X *    score up on the board.
  764. X */
  765. Xprpeg(score, peg, myturn)
  766. Xregister int    score;
  767. Xchar        peg;
  768. XBOOLEAN        myturn;
  769. X{
  770. X    register int    y, x;
  771. X
  772. X    if (!myturn)
  773. X        y = SCORE_Y + 2;
  774. X    else
  775. X        y = SCORE_Y + 5;
  776. X
  777. X    if (score <= 0 || score >= glimit) {
  778. X        if (peg == '.')
  779. X            peg = ' ';
  780. X        if (score == 0)
  781. X            x = SCORE_X + 2;
  782. X        else {
  783. X            x = SCORE_X + 2;
  784. X            y++;
  785. X        }
  786. X    }
  787. X    else {
  788. X        x = (score - 1) % 30;
  789. X        if (score > 90 || (score > 30 && score <= 60)) {
  790. X            y++;
  791. X            x = 29 - x;
  792. X        }
  793. X        x += x / 5;
  794. X        x += SCORE_X + 3;
  795. X    }
  796. X    mvaddch(y, x, peg);
  797. X    mvprintw(SCORE_Y + (myturn ? 7 : 1), SCORE_X + 10, "%3d", score);
  798. X}
  799. X
  800. X/*
  801. X * cdiscard -- the computer figures out what is the best discard for
  802. X * the crib and puts the best two cards at the end
  803. X */
  804. X
  805. Xcdiscard( mycrib )
  806. X
  807. X    BOOLEAN        mycrib;
  808. X{
  809. X    CARD            d[ CARDS ],  h[ FULLHAND ],  cb[ 2 ];
  810. X    register  int        i, j, k;
  811. X    int            nc, ns;
  812. X    long            sums[ 15 ];
  813. X    static  int        undo1[15]   = {0,0,0,0,0,1,1,1,1,2,2,2,3,3,4};
  814. X    static  int        undo2[15]   = {1,2,3,4,5,2,3,4,5,3,4,5,4,5,5};
  815. X
  816. X    makedeck( d );
  817. X    nc = CARDS;
  818. X    for( i = 0; i < knownum; i++ )  {    /* get all other cards */
  819. X        remove( known[i], d, nc-- );
  820. X    }
  821. X    for( i = 0; i < 15; i++ )  sums[i] = 0L;
  822. X    ns = 0;
  823. X    for( i = 0; i < (FULLHAND - 1); i++ )  {
  824. X        cb[0] = chand[i];
  825. X        for( j = i + 1; j < FULLHAND; j++ )  {
  826. X        cb[1] = chand[j];
  827. X        for( k = 0; k < FULLHAND; k++ )  h[k] = chand[k];
  828. X        remove( chand[i], h, FULLHAND );
  829. X        remove( chand[j], h, FULLHAND - 1 );
  830. X        for( k = 0; k < nc; k++ )  {
  831. X            sums[ns] += scorehand( h, d[k], CINHAND, TRUE, FALSE );
  832. X            if( mycrib )  sums[ns] += adjust( cb, d[k] );
  833. X            else      sums[ns] -= adjust( cb, d[k] );
  834. X        }
  835. X        ++ns;
  836. X        }
  837. X    }
  838. X    j = 0;
  839. X    for( i = 1; i < 15; i++ )  if(  sums[i] > sums[j]  )  j = i;
  840. X    for( k = 0; k < FULLHAND; k++ )  h[k] = chand[k];
  841. X    remove( h[ undo1[j] ], chand, FULLHAND );
  842. X    remove( h[ undo2[j] ], chand, FULLHAND - 1 );
  843. X    chand[4] = h[ undo1[j] ];
  844. X    chand[5] = h[ undo2[j] ];
  845. X}
  846. X
  847. X
  848. X
  849. X/*
  850. X * returns true if some card in hand can be played without exceeding 31
  851. X */
  852. X
  853. Xanymove( hand, n, sum )
  854. X
  855. X    CARD        hand[];
  856. X    int            n;
  857. X    int            sum;
  858. X{
  859. X    register  int        i, j;
  860. X
  861. X    if(  n < 1  )  return( FALSE );
  862. X    j = hand[0].rank;
  863. X    for( i = 1; i < n; i++ )  {
  864. X        if(  hand[i].rank < j  )  j = hand[i].rank;
  865. X    }
  866. X    return(  sum + VAL( j )  <=  31  );
  867. X}
  868. X
  869. X
  870. X
  871. X/*
  872. X * anysumto returns the index (0 <= i < n) of the card in hand that brings
  873. X * the s up to t, or -1 if there is none
  874. X */
  875. X
  876. Xanysumto( hand, n, s, t )
  877. X
  878. X    CARD        hand[];
  879. X    int            n;
  880. X    int            s,  t;
  881. X{
  882. X    register  int        i;
  883. X
  884. X    for( i = 0; i < n; i++ )  {
  885. X        if(  s + VAL( hand[i].rank )  ==  t  )  return( i );
  886. X    }
  887. X    return( -1 );
  888. X}
  889. X
  890. X
  891. X
  892. X
  893. X/*
  894. X * return the number of cards in h having the given rank value
  895. X */
  896. X
  897. Xnumofval( h, n, v )
  898. X
  899. X    CARD        h[];
  900. X    int            n;
  901. X    int            v;
  902. X{
  903. X    register  int        i, j;
  904. X
  905. X    j = 0;
  906. X    for( i = 0; i < n; i++ )  {
  907. X        if(  VAL( h[i].rank )  ==  v  )  ++j;
  908. X    }
  909. X    return( j );
  910. X}
  911. X
  912. X
  913. X
  914. X/*
  915. X * makeknown remembers all n cards in h for future recall
  916. X */
  917. X
  918. Xmakeknown( h, n )
  919. X
  920. X    CARD        h[];
  921. X    int            n;
  922. X{
  923. X    register  int        i;
  924. X
  925. X    for( i = 0; i < n; i++ )  {
  926. X        known[ knownum++ ] = h[i];
  927. X    }
  928. X}
  929. X
  930. END_OF_support.c
  931. if test 6894 -ne `wc -c <support.c`; then
  932.     echo shar: \"support.c\" unpacked with wrong size!
  933. fi
  934. # end of overwriting check
  935. fi
  936. echo shar: End of archive 2 \(of 2\).
  937. cp /dev/null ark2isdone
  938. MISSING=""
  939. for I in 1 2 ; do
  940.     if test ! -f ark${I}isdone ; then
  941.     MISSING="${MISSING} ${I}"
  942.     fi
  943. done
  944. if test "${MISSING}" = "" ; then
  945.     echo You have unpacked both archives.
  946.     rm -f ark[1-9]isdone
  947. else
  948.     echo You still need to unpack the following archives:
  949.     echo "        " ${MISSING}
  950. fi
  951. ##  End of shell archive.
  952. exit 0
  953.